package com.easychat;

import com.easychat.redis.RedisComponent;
import com.easychat.websocket.netty.NeeyWebSocketStarter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author 10 2024/10/18
 */
@Component("initRun")
public class InitRun implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(InitRun.class);
    @Resource
    private DataSource dataSource;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private NeeyWebSocketStarter neeyWebSocketStarter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try{
            dataSource.getConnection();
            redisTemplate.opsForValue().get("test");
            new Thread(neeyWebSocketStarter).start();
            logger.info("服务启动成功,可以开始愉快的开发了");
        }catch (SQLException e){
            logger.error("数据库连接失败", e);
        }catch (RedisConnectionFailureException e){
            logger.error("redis配置错误，请检测redis配置", e);
        }catch (Exception e){
            logger.error("服务启动失败", e);
        }
    }
}
